exports.connectToDatabase = function connectToDatabase(eHandler, req, res) {

    var mysql = require('mysql');

    var afDetails = process.env.VCAP_SERVICES != null ? JSON.parse(process.env.VCAP_SERVICES)["mysql-5.1"][0]["credentials"] : null;

    var connection = afDetails === null ?

        mysql.createConnection({
            host: 'localhost',
            user: 'root',
            database: 'battleshipsdb'
        }) :

        mysql.createConnection({

            host: afDetails['host'],
            user: afDetails['user'],
            port: afDetails['port'],
            database: 'battlshipsdb',
            password: afDetails['password']
        });

    connection.connect(function (err) {

        if (err) {

            connection = null;
            eHandler(err, req, res);
        }
    });

    return connection;
};